package com.senriot.quickup.config.payment

import com.egzosn.pay.ali.api.AliPayConfigStorage
import com.egzosn.pay.ali.api.AliPayService
import com.egzosn.pay.common.bean.CertStoreType
import com.egzosn.pay.common.http.HttpConfigStorage
import com.egzosn.pay.common.util.sign.SignUtils
import com.egzosn.pay.wx.api.WxPayConfigStorage
import com.egzosn.pay.wx.api.WxPayService
import com.senriot.quickup.config.payment.interceptor.AliPayInterceptor
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration


@Configuration
class AlipayConfiguration
{
//    var appId: String = ""
//    var privateKey: String = ""
//    var publicKey: String = ""
//    var serverUrl: String = ""
//    var notifyUrl: String = ""
//    var returnUrl: String = ""

    @Bean
    fun aliPayConfigStorage(): AliPayConfigStorage
    {

        return AliPayConfigStorage().apply {
            pid = "2088031023169935"
            appid = "2018050202621586"
            keyPrivate =
                    "MIICdAIBADANBgkqhkiG9w0BAQEFAASCAl4wggJaAgEAAoGBAKL8HvmpzI9DEevqD0D0CrTIzvcHE/p+BqYY8Vb8o9J9Jy2Sj6LvlUefEWgR+VA3kIYTr1FGBVp4lElsrhGpmB9SybWHteLzCjGeVTEauFWdw2sCjSpBFV2jHwNvwDEN2GQUn1qdvCJc+TDRdqmUi3pQ1MqfYWAPKEMWCEuju6SDAgMBAAECf36xfWKVm23BtSytxS24hpoBfuCHoAAX9xMmUHRN7wqwPsQuKXl+w4vpGp7LWgIBL1cJ9j43L/IjySFKWgVgz95GVq0ADIY4GkSTJs9pfca+6Wfoj50GHryGJnSQv1QK+uGfqZHGT0hzgoaBGhkG6iJcQhmfYMLZbDeSUQ05qiECQQD2UCxmPDSWOqZAjumpGLMIQzOK4dVMApJmNBqf3Knpz23m2HC1Y+nXv2sc5m6fVutstS0KoUCZ58S868FlFw/xAkEAqWUEMURDPmqTf/f0YxW+lJ2cCl15guyCXoK8Jxr7MICygftos8GiUL+XYCNlPoQcC3+BbtQ2UkggcidQ8dJvswJAdGLHx/sUwhOucPzWbd9M4U+XsD0ro0Hn2UEvf3AuHVu1S4nkH+G5IgfB3iMIDGRu06v8P3SYaOC59qbTeCfgkQJBAIBx5pHs7wg/DAU24QrZDB/ous9pPaA28kvz2fPVE68dhz3kgun647dS79eoPzb+Ob3GO0DsjRwrMN8eTNRw+t0CQC0Aoy6TVDHNOb3Aou2VgwMmj63ANQlVzOfMzvYw/+cetKvJy1Ddg+YFxdJSQQxPQ5ucX2C5uVEmlPJm488E9nU="
            keyPublic =
                    "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB"
            notifyUrl = "https://new.kuaixing.vip/api/payment/ali_notify_url"
            returnUrl = "https://new.kuaixing.vip/api/payment/ali_notify_url"
            signType = SignUtils.RSA.name
            inputCharset = "utf-8"
            seller = "2088031023169935"
            isTest = false
        }
    }


    @Bean
    fun aliPayService(config: AliPayConfigStorage): AliPayService
    {
        val httpConfigStorage = HttpConfigStorage()
        httpConfigStorage.maxTotal = 20;
        httpConfigStorage.defaultMaxPerRoute = 10;
        val service = AliPayService(config, httpConfigStorage)
        service.addPayMessageInterceptor(AliPayInterceptor())
        return service
    }
}

@Configuration
@ConfigurationProperties(prefix = "wxpay")
class WxPayConfiguration
{
    var appId: String = ""
    var appSecret: String = ""
    var mchId: String = ""
    var privateKey: String = ""
    var certPath: String = ""
    var returnUrl: String = ""
    var notifyUrl: String = ""


    override fun toString(): String
    {
        return "WxPayConfiguration(appId='$appId', appSecret='$appSecret', mchId='$mchId', certPath='$certPath', returnUrl='$returnUrl', notifyUrl='$notifyUrl')"
    }


    @Bean
    fun wxPayConfig() = WxPayConfigStorage().apply {
        this.appid = this@WxPayConfiguration.appId
        this.mchId = this@WxPayConfiguration.mchId
        this.secretKey = this@WxPayConfiguration.appSecret
        this.notifyUrl = this@WxPayConfiguration.notifyUrl
        this.returnUrl = this@WxPayConfiguration.returnUrl
        this.keyPrivate = this@WxPayConfiguration.privateKey
        inputCharset = "utf-8"
        signType = "MD5"
    }

    @Bean
    fun httpConfigStorage(): HttpConfigStorage
    {
        val inputStream = this.javaClass.getResourceAsStream("/certificate/apiclient_cert.p12")
        return HttpConfigStorage().apply {
            setKeystore(inputStream)
            storePassword = "1498779822"
            certStoreType = CertStoreType.INPUT_STREAM
        }
    }

    @Bean
    fun wxPayService(config: WxPayConfigStorage, httpConfigStorage: HttpConfigStorage) =
            WxPayService(config, httpConfigStorage)
}
